package com.cecurs.bigexcel;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

public class ExcelUtil {

    private CellStyle titleStyle;

    private CellStyle dataStyle;

    public SXSSFWorkbook getWorkbook(int rowCache) {

        //创建workbook，窗口大小默认设置为100，即每100行写入本地磁盘
        SXSSFWorkbook workbook = new SXSSFWorkbook( rowCache );
        //开启本地临时文件，用本地磁盘换取内存空间，防止OOM
        workbook.setCompressTempFiles( true );

        //创建表头单元格格式，并设置居中,设置自动换行
        titleStyle = workbook.createCellStyle();
        titleStyle.setAlignment( HorizontalAlignment.CENTER );
        titleStyle.setVerticalAlignment( VerticalAlignment.CENTER );
        titleStyle.setBorderBottom( BorderStyle.THIN );
        titleStyle.setBorderTop( BorderStyle.THIN );
        titleStyle.setBorderLeft( BorderStyle.THIN );
        titleStyle.setBorderRight( BorderStyle.THIN );
        titleStyle.setFillForegroundColor( IndexedColors.GREY_40_PERCENT.getIndex() );
        titleStyle.setFillPattern( FillPatternType.SOLID_FOREGROUND );
        titleStyle.setWrapText( true );

        Font font = workbook.createFont();
        font.setFontName( "黑体" );
        font.setBold( true );
        font.setFontHeightInPoints( (short) 12 );
        titleStyle.setFont( font );

        //创建单元格格式，并设置居中,设置自动换行
        dataStyle = workbook.createCellStyle();
        dataStyle.setAlignment( HorizontalAlignment.CENTER );
        dataStyle.setVerticalAlignment( VerticalAlignment.CENTER );
        dataStyle.setBorderBottom( BorderStyle.THIN );
        dataStyle.setBorderTop( BorderStyle.THIN );
        dataStyle.setBorderLeft( BorderStyle.THIN );
        dataStyle.setBorderRight( BorderStyle.THIN );
        dataStyle.setWrapText( true );

        //返回文档实例
        return workbook;
    }

    public SXSSFWorkbook getWorkbook(CellStyle titleStyle, CellStyle dataStyle) {
        this.titleStyle = titleStyle;
        this.dataStyle = dataStyle;
        //创建workbook，窗口大小设置为100，即每100行写入本地磁盘
        SXSSFWorkbook workbook = new SXSSFWorkbook( 100 );
        //开启本地临时文件，用本地磁盘换取内存空间，防止OOM
        workbook.setCompressTempFiles( true );
        //返回文档实例
        return workbook;
    }

    public void fillTitle(SXSSFSheet sheet, String[] title) {
        //创建第一行，用于填充标题
        SXSSFRow titleRow = sheet.createRow( 0 );
        titleRow.setHeight( (short) 600 );
        //填充标题
        for (int i = 0; i < title.length; i++) {
            sheet.setColumnWidth( i, 20 * 256 );
            //创建单元格
            SXSSFCell cell = titleRow.createCell( i );
            //设置单元格内容
            cell.setCellValue( title[i] );
            //设置单元格样式
            cell.setCellStyle( titleStyle );
        }
    }

    /**
     * 填充内容
     *
     * @param sheet
     * @param rowContent
     */
    public void fillData(SXSSFSheet sheet, String[] rowContent) {
        //创建行
        SXSSFRow row = sheet.createRow( sheet.getLastRowNum() + 1 );
        //遍历某一行
        for (int j = 0; j < rowContent.length; j++) {
            //创建单元格
            SXSSFCell cell = row.createCell( j );
            //设置单元格内容
            cell.setCellValue( rowContent[j] );
            //设置单元格样式
            cell.setCellStyle( dataStyle );
        }
    }

    public SXSSFSheet createSheet(SXSSFWorkbook workBook, String sheetName) {
        SXSSFSheet sheet = workBook.createSheet( sheetName );
        return sheet;
    }
}
